/***************************************************************************
*   Copyright (C) 2010-2011 by swkyer <swkyer@gmail.com>                  *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/
#include "stdafx.h"
#include <windows.h>
#include "../JlinkMIPS/Ejtag.h"

#ifdef _DEBUG
#pragma comment (lib, "../debug/jlinkmips.lib")
#else
#pragma comment (lib, "../release/jlinkmips.lib")
#endif


int test_Ejtag(int dev)
{
	int retv;
	uint32_t idcode;
	ejtag_t *pejtag;

	pejtag = ejtag_init(dev);
	if (pejtag == NULL)
	{
		printf("faild to open Jlink...\n");
		return -1;
	}

	jlink_unknow_eb_t ueb;
	retv = jlinkusb_get_unknow_eb(pejtag->usbdev, &ueb);
	jlink_unknow_c6_t uc6;
	retv = jlinkusb_get_unknow_c6(pejtag->usbdev, &uc6);


	retv = ejtag_tap_reset(pejtag, 1);
	if (retv != 0)
	{
		printf("faild to reset tap using TRST.\n");
		goto _out;
	}

	retv = ejtag_tap_reset(pejtag, 0);
	if (retv != 0)
	{
		printf("faild to reset tap using 5 time TMS=1.\n");
		goto _out;
	}
	retv = ejtag_runtest_idle(pejtag);
	if (retv != 0)
	{
		printf("faild to go into IDLE.\n");
		goto _out;
	}

	
	retv = ejtag_get_idcode(pejtag, &idcode);
	if (retv != 0)
	{
		printf("faild to get IDCODE.\n");
		goto _out;
	}
	printf("IDCODE: 0x%08x.\n", idcode);

	retv = ejtag_get_impcode(pejtag, &idcode);
	if (retv != 0)
	{
		printf("faild to get IMPCODE.\n");
		goto _out;
	}
	printf("IMPCODE: 0x%08x.\n", idcode);


_out:
	ejtag_exit(pejtag);
	return 0;
}

